<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header>
      <el-image style="width: 313px; height: 50px" :src="require('../../assets/img/html/logo.png')" fit="contain"/>
      <h3>智慧党建平台</h3>
      <el-button color="#5cb3cc" @click="logout" style="color: white">退出登录</el-button>
    </el-header>
    <!-- 页面主体区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="isCollapse ? '64px' : '200px'">
        <div class="toggle-button" @click="toggleCollapse">
          <el-icon>
            <fold/>
          </el-icon>
        </div>
        <!-- 侧边栏菜单区域 -->
        <el-menu active-text-color="rgb(251, 185, 87)"
                 background-color="#4762B0"
                 text-color="#cdcdcd"
                 unique-opened
                 :collapse="isCollapse"
                 :collapse-transition="false"
                 router
                 :default-active="activePath">
          <!-- 一级菜单 -->
          <el-menu-item :index="'/index'" @click="saveNavState('/index')" :key="1">
            <el-icon><house /></el-icon>
            <span>首页</span>
          </el-menu-item>
          <el-sub-menu :index="item.id + ''" v-for="item in menulist" :key="item.id">
            <!-- 一级菜单的模板区域 -->
            <template #title>
              <el-icon>
                <component :is="item.icon"/>
              </el-icon>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单 -->
            <el-menu-item :index="subItem.path" v-for="subItem in item.children" :key="subItem.id"
                          @click="saveNavState(subItem.path)">
              <template #title>
                <!--                <el-icon><location/></el-icon>-->
                <!-- 文本 -->
                <el-badge v-if="subItem.path === '/advice'" :value="v" :max="99" class="item">
                  <span>{{ subItem.itemname }}</span>
                </el-badge>
                <span v-else>{{ subItem.itemname }}</span>
              </template>
            </el-menu-item>
          </el-sub-menu>
        </el-menu>
      </el-aside>
      <!-- 右侧内容主体 -->
      <el-main>
        <div class="el_border">
          <!-- 面包屑导航区域 -->
          <div>
            <div style="display:flex;justify-content: space-between">
              <Breadcrumb v-on:activePath="saveNavState"></Breadcrumb>
              <el-button v-if="activePath == '/mydeclare'" color="#5cb3cc" @click="dialogVisible = true" style="color:white">个人汇报</el-button>
            </div>
          </div>
          <!-- 路由占位符 -->
          <router-view/>
        </div>
      </el-main>
    </el-container>
  </el-container>

  <el-dialog title="个人汇报" @opened="myEcharts" v-model="dialogVisible" width="45%" center>
    <div class="Echarts">
      <div id="myechars" style="width: 600px; height: 400px"></div>
    </div>
  </el-dialog>

</template>

<script>
import axios from "axios";
import Breadcrumb from "@/components/Breadcrumb";
import * as echarts from "echarts";

export default {
  components: {Breadcrumb},
  data() {
    return {
      // 左侧菜单数据
      menulist: [
        {
          id: 2,
          authName: '我的活动',
          icon: 'menu',
          children: [{
            id: 1,
            itemname: '我的收藏',
            path: '/collection'
          }, {
            id: 2,
            itemname: '已提交',
            path: '/submitted'
          }, {
            id: 3,
            itemname: '已申报',
            path: '/declared'
          }
          ]
        },
        {
          id: 3,
          authName: '审核状态',
          icon: 'Tickets',
          children: [{
            id: 1,
            itemname: '待审核',
            path: '/state'
          },
            {
              id: 2,
              itemname: '已发布结果',
              path: '/stated'
            }
          ]
        },
        {
          id: 4,
          authName: '项目意见',
          icon: 'MessageBox',
          children: [{
            id: 1,
            itemname: '查看意见',
            path: '/advice'
          }
          ]
        },
        {
          id: 5,
          authName: '个人信息',
          icon: 'UserFilled',
          children: [{
            id: 1,
            itemname: '个人资料',
            path: '/information'
          },
            {
              id: 2,
              itemname: '修改密码',
              path: '/editpwd'
            },
          ]
        },
      ],
      // 是否折叠
      isCollapse: false,
      // 被激活的链接地址
      activePath: '',
      dialogVisible: false,
      v:'',
    }
  },
  created() {
    this.activePath = window.sessionStorage.getItem('activePath')
    this.get()
  },
  methods: {
    logout() {
      axios.get('/students/logout', {}).then(res => {
        window.sessionStorage.clear()
        let r = res.data
        alert(r.msg)
        this.$router.push('/')
      }).catch(err => {
        console.log(err)
      })
    },

    // 点击按钮，切换菜单的折叠与展开
    toggleCollapse() {
      this.isCollapse = !this.isCollapse
    },
    // 保存链接的激活状态
    saveNavState(activePath) {
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
      if (activePath === '/advice' && this.v != '')
        this.clear()
    },

    myEcharts() {
      var myChart = echarts.init(document.getElementById("myechars"));
      //配置图表
      var option = {
        title: {
          text: '项目申报情况',
          left: 'center',
          top: 20,
          textStyle: {
            color: 'black'
          }
        },
        tooltip: {
          trigger: 'item'
        },
        visualMap: {
          show: false,
          min: 0,
          max:20,
          inRange: {
            colorLightness: [0, 1]
          }
        },
        series: [
          {
            name: '',
            type: 'pie',
            radius: '55%',
            center: ['50%', '50%'],
            data: [],
            roseType: 'radius',
            label: {
              color: 'black'
            },
            labelLine: {
              lineStyle: {
                color: 'blue'
              },
              smooth: 0.2,
              length: 10,
              length2: 20
            },
            itemStyle: {
              // shadowBlur: 200,
              // color: '#c23531',
              // shadowColor: 'rgba(0, 0, 0, 0.5)'
            },
            animationType: 'scale',
            animationEasing: 'elasticOut',
            animationDelay: function (idx) {
              return Math.random() * 200;
            }
          }
        ]
      };
      myChart.setOption(option);
      axios.post("/project/analysis").then((res) => {
        let r = res.data;
        if (r.flag === true) {
          myChart.setOption({  //动画的配置
            series: [{
              data: r.datas.sort(function (a, b) {
                return a.value - b.value;
              })  //这里数据是一个数组的形似
            }]
          })
        } else {
          alert(r.msg);
        }
      }).catch((err) => {
        console.log(err);
      });
    },
    get(){
      axios.post('/project/getValue').then(res => {
        let r = res.data
        this.v = r.v
      }).catch(err => {
        console.log(err)
      })
    },
    clear(){
      axios.post('/project/clear').then(res => {
        let r = res.data
        this.v = r.v
      }).catch(err => {
        console.log(err)
      })
    },
  }
}
</script>

<style scoped>
.home-container {
  height: 100%;
}

.el-header {
  background: #4762B0;
  display: flex;
  justify-content: space-between;
  padding-left: 0;
  align-items: center;
  color: #fff;
  font-size: 20px;
}

span {
  margin-left: 15px;
}

.el-aside {
  background: #4762B0;
}

.el-menu {
  border-right: none;
}

.toggle-button {
  background: #4762B0;
  font-size: 25px;
  line-height: 24px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}

/*内容主体区*/
/*内容主体*/
.el-main {
  background-color: #e9eef3;
}

.el_border {
  background-color: white;
  padding: 20px 10px 10px 30px;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.12), 0 0 6px rgba(0, 0, 0, 0.04);
}
</style>
